package com.liuyu.lottery.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.liuyu.common.core.core.domain.R;
import com.liuyu.common.core.utils.MapstructUtils;
import com.liuyu.common.orm.core.page.PageQuery;
import com.liuyu.common.orm.core.page.TableDataInfo;
import com.liuyu.common.orm.core.service.impl.BaseServiceImpl;
import com.liuyu.lottery.domain.table.MailTableDef;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.liuyu.lottery.mapper.MailMapper;
import com.liuyu.lottery.domain.Mail;
import com.liuyu.lottery.domain.bo.MailBo;
import com.liuyu.lottery.domain.vo.MailVo;
import com.liuyu.lottery.service.IMailService;
import static com.liuyu.lottery.domain.table.MailTableDef.MAIL;

/**
 * MailControllerService业务层处理
 *
 * @author liuyu
 * 2024-03-05
 */
@Service
public class MailServiceImpl extends BaseServiceImpl<MailMapper, Mail> implements IMailService
{
    @Resource
    private MailMapper mailMapper;

    @Override
    public QueryWrapper query() {
        return super.query().from(MAIL);
    }

    private QueryWrapper buildQueryWrapper(MailBo mailBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        queryWrapper.and(MAIL.HOST.eq(mailBo.getHost()));
        queryWrapper.and(MAIL.PORT.eq(mailBo.getPort()));
        queryWrapper.and(MAIL.FROM_MAIL.eq(mailBo.getFromMail()));
        queryWrapper.and(MAIL.PASS.eq(mailBo.getPass()));
        queryWrapper.and(MAIL.TO_MAIL.eq(mailBo.getToMail()));
        queryWrapper.and(MAIL.SSL_ENABLE.eq(mailBo.getSslEnable()));

        return queryWrapper;
    }

    /**
     * 查询MailController
     *
     * @param id MailController主键
     * @return MailController
     */
    @Override
    public MailVo selectById(Integer id)
    {
        return this.getOneAs(query().where(MAIL.ID.eq(id)), MailVo.class);

    }

    /**
     * 查询MailController列表
     *
     * @param mailBo MailControllerBo
     * @return MailController集合
     */
    @Override
    public List<MailVo> selectList(MailBo mailBo)
    {
        QueryWrapper queryWrapper = buildQueryWrapper(mailBo);
        return this.listAs(queryWrapper, MailVo.class);
    }

    /**
     * 分页查询MailController列表
     *
     * @param mailBo MailControllerBo
     * @return 分页MailController集合
     */
    @Override
    public TableDataInfo<MailVo> selectPage(MailBo mailBo)
    {
        QueryWrapper queryWrapper = buildQueryWrapper(mailBo);
        Page<MailVo> page = this.pageAs(PageQuery.build(), queryWrapper, MailVo.class);
        return TableDataInfo.build(page);
    }

    /**
     * 新增MailController
     *
     * @param mailBo MailControllerBo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insert(MailBo mailBo)
    {
        Mail mail = MapstructUtils.convert(mailBo, Mail.class);

        return this.save(mail);//使用全局配置的雪花算法主键生成器生成ID值
    }

    /**
     * 修改MailController
     *
     * @param mailBo MailControllerBo
     * @return 结果:true 更新成功，false 更新失败
     */
    @Override
    public boolean update(MailBo mailBo)
    {
        Mail mail = MapstructUtils.convert(mailBo, Mail.class);
        if(ObjectUtil.isNotNull(mail) && ObjectUtil.isNotNull(mail.getId())) {
            boolean updated = this.updateById(mail);
            return updated;
        }
        return false;
    }

    /**
     * 批量删除MailController
     *
     * @param ids 需要删除的MailController主键集合
     * @return 结果:true 删除成功，false 删除失败
     */
    @Transactional
    @Override
    public boolean deleteByIds(Integer[] ids)
    {
        return this.removeByIds(Arrays.asList(ids));
    }

    @Override
    public R<List<MailBo>> search(String fromUser, String toUser) {
        List<MailBo> mailBos = this.queryChain().select(MAIL.ALL_COLUMNS).from(MAIL)
            .like(Mail::getFromMail, fromUser, StrUtil.isNotEmpty(fromUser))
            .like(Mail::getToMail, toUser, StrUtil.isNotEmpty(toUser))
            .listAs(MailBo.class);
        return R.ok(mailBos);
    }

}
